﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MvcCoffeeShop
{
    namespace DataAccess
    {

        public class DBSafeConvert : IDisposable
        {
            //Isaac Ortiz 5/2/2013: Safely set fields so DBNulls do not cause issues.
             

            bool IsDBNull(object oField)
            {
                if (oField is DBNull)
                {
                    return true;
                }
                else
                {
                    return false;
                }

            }



            public bool? SafeBool(object oField)
            {

                if (IsDBNull(oField))
                {
                    return null;
                }
                else
                {
                    return Convert.ToBoolean(oField);
                }

            }

            public byte? SafeByte(object oField)
            {
                if (IsDBNull(oField))
                {
                    return null;
                }
                else
                {
                    return Convert.ToByte(oField);
                }
            }

            public char? SafeChar(object oField)
            {
                if (IsDBNull(oField))
                {
                    return null;
                }
                else
                {
                    return Convert.ToChar(oField);
                }
            }

            public DateTime? SafeDateTime(object oField)
            {
                if (IsDBNull(oField))
                {
                    return null;
                }
                else
                {
                    return Convert.ToDateTime(oField);
                }
            }

            public decimal? SafeDecimal(object oField)
            {
                if (IsDBNull(oField))
                {
                    return null;
                }
                else
                {
                    return Convert.ToDecimal(oField);
                }
            }

            public int? SafeInt(object oField)
            {
                if (IsDBNull(oField))
                {
                    return null;
                }
                else
                {
                    return Convert.ToInt32(oField);
                }
            }

            public Int16? SafeInt16(object oField)
            {
                if (IsDBNull(oField))
                {
                    return null;
                }
                else
                {
                    return Convert.ToInt16(oField);
                }
            }

            public Int32? SafeInt32(object oField)
            {
                if (IsDBNull(oField))
                {
                    return null;
                }
                else
                {
                    return Convert.ToInt32(oField);
                }
            }

            public Int64? SafeInt64(object oField)
            {
                if (IsDBNull(oField))
                {
                    return null;
                }
                else
                {
                    return Convert.ToInt64(oField);
                }
            }

            public double? SafeDouble(object oField)
            {
                if (IsDBNull(oField))
                {
                    return null;
                }
                else
                {
                    return Convert.ToDouble(oField);
                }
            }

            public sbyte? SafeSbyte(object oField)
            {
                if (IsDBNull(oField))
                {
                    return null;
                }
                else
                {
                    return Convert.ToSByte(oField);
                }
            }

            public Single? SafeSingle(object oField)
            {
                if (IsDBNull(oField))
                {
                    return null;
                }
                else
                {
                    return Convert.ToSingle(oField); 
                }
            }

            public string SafeString(object oField)
            {
                if (IsDBNull(oField))
                {
                    return string.Empty;
                }
                else
                {
                    return Convert.ToString(oField);
                }
            }


            public UInt16? SafeUInt16(object oField)
            {
                if (IsDBNull(oField))
                {
                    return null;
                }
                else
                {
                    return Convert.ToUInt16(oField);
                }
            }

            public UInt32? SafeUInt32(object oField)
            {
                if (IsDBNull(oField))
                {
                    return null;
                }
                else
                {
                    return Convert.ToUInt32(oField);
                }
            }

            public UInt64? SafeUInt64(object oField)
            {
                if (IsDBNull(oField))
                {
                    return null;
                }
                else
                {
                    return Convert.ToUInt64(oField);
                }
            }


            public void Dispose()
            {
                throw new NotImplementedException();
            }
        }
    }
}